﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using BaseCardTypes = Wormhole.Engine.CardLibrary.BaseCardTypes;

namespace Wormhole.UI.Application.Controls.Cards
{
	/// <author>Rodney McKay</author>
	/// <date>08 April 2008</date>
	/// <summary>
	/// This control displays a mission card.
	/// </summary>
	public class MissionCard : Card
	{
		#region Constructor
		public MissionCard() : base()
		{
		}

		public MissionCard(BaseCardTypes.Card card)
			: base(card)
		{
			card.Owner.Game.Mission.StatusChanged += new Wormhole.Engine.Mission.Mission.StatusChangedDelegate(Mission_StatusChanged);
			card.Owner.Game.Mission.DifficultyChanged += new Wormhole.Engine.Mission.Mission.TotalChangedDelegate(Mission_DifficultyChanged);
		}
		#endregion

		#region Event Handlers
		/// <summary>
		/// Mission Status Changed
		/// </summary>
		/// <param name="status"></param>
		private void Mission_StatusChanged(Wormhole.Engine.Mission.MissionStatus status)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayImage();
			});
		}

		/// <summary>
		/// Mission Difficulty Changed.
		/// </summary>
		/// <param name="total"></param>
		private void Mission_DifficultyChanged(int total)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
				{
					this.DisplayImage();
				});
		}
		#endregion

		#region Set Size
		/// <summary>
		/// Set the card size.
		/// </summary>
		protected override void SetSize()
		{
			this.cardImage.Width = 313 * .9;
			this.cardImage.Height = 217 * .9;
			this.cardImage.Stretch = Stretch.Fill;

			this.outsideBorder.Width = this.cardImage.Width + 10;
			this.outsideBorder.Height = this.cardImage.Height + 10;

			this.Width = this.outsideBorder.Width;
			this.Height = this.outsideBorder.Height;
		}
		#endregion

		#region Display Image
		/// <summary>
		/// Display the card image.
		/// </summary>
		protected override void DisplayImage()
		{
			// get the drawing context
			DrawingVisual drawingVisual = new DrawingVisual();
			DrawingContext drawingContext = drawingVisual.RenderOpen();

			// load the images that will blank out values
			BitmapSource blankDifficulty = new BitmapImage(new Uri("pack://application:,,,/Wormhole.UI.Application;component/images/blankDifficulty.png"));

			// draw the card image
			drawingContext.DrawImage(this.imageSource, new Rect(0, 0, this.imageSource.Width, this.imageSource.Height));

			// clear the difficulty dislay
			drawingContext.DrawImage(blankDifficulty, new Rect(39, 32, 46, 36));

			// create the skills font type
			Typeface baseFont = new Typeface("Veranda");
			Typeface skillsFont = new Typeface(baseFont.FontFamily, FontStyles.Normal, FontWeights.Bold, FontStretches.Normal);

			// draw the difficulty text
			Brush difficultyBrush = new SolidColorBrush(Colors.Black);
			if (this.Game.Mission.Difficulty > this.Game.Mission.CurrentMission.Difficulty)
				difficultyBrush = new SolidColorBrush(Colors.Red);
			else if (this.Game.Mission.Difficulty > this.Game.Mission.CurrentMission.Difficulty)
				difficultyBrush = new SolidColorBrush(Colors.Blue);

			difficultyBrush.Opacity = 1;

			drawingContext.DrawText(
				   new FormattedText(this.Game.Mission.Difficulty.ToString(),
					  CultureInfo.GetCultureInfo("en-us"),
					  FlowDirection.LeftToRight,
					  skillsFont,
					  25, difficultyBrush),
					  new Point(45, 35));


			// the success brush
			Brush brush = new SolidColorBrush(Colors.Red);
			brush.Opacity = 0.8;

			// draw the text
			if (this.EngineCard.Owner.Game.Mission.Status == Wormhole.Engine.Mission.MissionStatus.Success)
			{
				drawingContext.DrawText(
					   new FormattedText("SUCCESS!!!",
						  CultureInfo.GetCultureInfo("en-us"),
						  FlowDirection.LeftToRight,
						  skillsFont,
						  60, brush),
						  new Point(3, 65));
			}

			// get the image source
			drawingContext.Close();
			RenderTargetBitmap targetBitmap = new RenderTargetBitmap(this.imageSource.PixelWidth, this.imageSource.PixelHeight, 96, 96, PixelFormats.Pbgra32);
			targetBitmap.Render(drawingVisual);

			// assign the image source
			this.cardImage.Source = targetBitmap;
		}
		#endregion
	}
}
